import {
  getMealMoney,
  addMealMoney
} from '../../request/api'
import Toast from '@vant/weapp/toast/toast'
import utils from '../../utils/socketTask'
Page({

  data: {
    price: null,
    loading: false,
    mealMoney: 0,
    addMealShow: false,
    priceArr: [
      {
        name: '10',
        desc: '元'
      },
      {
        name: '30',
        desc: '元'
      },
      {
        name: '50',
        desc: '元'
      },
      {
        name: '100',
        desc: '元'
      },
      {
        name: '自定义',
        desc: ''
      },
    ],
    itemActive: 0,
    fieldShow: false
  },
  addClass(e) {
    let index = e.currentTarget.dataset.index
    let item = e.currentTarget.dataset.item
    this.setData({
      itemActive: index,
    })
    if (index == (this.data.priceArr.length-1)) {
      this.setData({
        price: null
      })
    } else {
      this.setData({
        price: item
      })
    }
    if (index == (this.data.priceArr.length-1)) {
      console.log('自定义')
      this.setData({
        fieldShow: true
      })
    } else {
      this.setData({
        fieldShow: false
      })
    }
  },
  priceChange(e) {
    if (!(/(^[0-9]*$)/.test(e.detail)) && !/[\.]/.test(e.detail)) {
      wx.showToast({
        title: '只能输入整数或小数',
        icon: 'none'
      })
      return
    }
    this.setData({
      price: e.detail
    })
  },
  getMealMoneyData() {
    getMealMoney().then(res => {
      if (res.code == 200) {
        console.log('饭卡余额', res)
        this.setData({
          mealMoney: res.data
        })
      }
    })
  },
  showAddMealMoneyData() {
    this.setData({
      addMealShow: true,
      price: this.data.priceArr[this.data.itemActive].name
    })
  },
  hideAddMealShow() {
    this.setData({
      addMealShow: false
    })
  },
  addMealMoneyData() {
    let obj = {
      money: this.data.price
    }
    if (this.data.itemActive !== this.data.priceArr.length - 1) {
      this.setData({
        loading: true
      })
      addMealMoney(obj).then(res => {
        if (res.code == 200) {
          console.log('充值饭卡', res)
          wx.showToast({
            title: '充值成功',
            icon: 'success'
          })
          this.setData({
            loading: false,
            addMealShow: false
          })
          this.getMealMoneyData()
        }
      })
    } else {
      if (this.data.price == null) {
        Toast({
          type: 'fail',
          message: '请输入金额',
          duration: 3000
        })
        return
      } else {
        this.setData({
          loading: true
        })
        addMealMoney(obj).then(res => {
          if (res.code == 200) {
            console.log('充值饭卡', res)
            wx.showToast({
              title: '充值成功',
              icon: 'success'
            })
            this.setData({
              loading: false,
              addMealShow: false
            })
            this.getMealMoneyData()
          }
  
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getMealMoneyData()
    utils.socketTask()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})